Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Scorm file upload error #79

Merged
merged 1 commit into from
Jul 1, 2024

Conversation

talhaaslam01
Copy link
Contributor

@talhaaslam01 talhaaslam01 commented Jun 25, 2024

Description

Some of my Scorm files (client's courses) are not being uploaded because, during extraction, some of the filenames are ending with \\ double backslash. But, the code ignores only the filenames ending with / forward slash.

@ziafazal ziafazal self-requested a review June 27, 2024 12:43
Copy link
Collaborator

@ziafazal ziafazal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@talhaaslam01 Thanks for creating PR. I have a suggestion if you can look into it that would be great.

@@ -384,7 +384,7 @@ def extract_package(self, package_file):
# the is_dir() method to verify whether a ZipInfo object points to a
# directory.
# https://docs.python.org/3.6/library/zipfile.html#zipfile.ZipInfo.is_dir
if not zipinfo.filename.endswith("/"):
if not (zipinfo.filename.endswith("/") or zipinfo.filename.endswith("\\")):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if you use is_dir method to check if file is directory or not as mentioned in the inline comment since python 3.6+ is supported now?

# Do not unzip folders, only files. In Python 3.6 we will have access to
                    # the is_dir() method to verify whether a ZipInfo object points to a
                    # directory.
                    # https://docs.python.org/3.6/library/zipfile.html#zipfile.ZipInfo.is_dir

Copy link
Contributor Author

@talhaaslam01 talhaaslam01 Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ziafazal, the is_dir method checks for / at the end of the filename which is the same as condition zipinfo.filename.endswith("/") currently being used.

'is_dir' method implementation:

def is_dir(self):
    """Return True if this archive member is a directory."""
    return self.filename[-1] == '/'

From python docs:

ZipInfo.is_dir()
    Return True if this archive member is a directory.

    This uses the entry’s name: directories should always end with /.

    New in version 3.6.

And, I have found this path in filenames filename: 'plugins\\plugins\\' in some of my scorm zip files.

Screenshot 2024-06-28 at 4 32 37 AM

Also, it is weird that filenames are using backslashes \\ in the path but the is_dir method is checking for / forwardslash only.

Copy link
Collaborator

@ziafazal ziafazal Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@talhaaslam01 it appears the issue lies with archives created using Windows OS having older version of PowerShell. That underlying issue appears to be fixed in PowerShell and there is new is_dir method which addresses the issue in python. I'd suggest backporting is_dir method in our codebase until we upgrade to python 3.12.3 or greater which has a fix for windows based archives. Please add an inline TODO comment about removing the method once we upgrade to python 3.12.3 or greater.

Suggested change
if not (zipinfo.filename.endswith("/") or zipinfo.filename.endswith("\\")):
def is_dir(zipinfo):
"""Return True if this archive member is a directory."""
if zipinfo.filename.endswith('/'):
return True
# The ZIP format specification requires to use forward slashes
# as the directory separator, but in practice some ZIP files
# created on Windows can use backward slashes. For compatibility
# with the extraction code which already handles this:
if os.path.altsep:
return zipinfo.filename.endswith((os.path.sep, os.path.altsep))
return False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ziafazal, I have updated the code.

@talhaaslam01 talhaaslam01 force-pushed the talha/upload-error branch 3 times, most recently from bca5fe8 to 0debe81 Compare June 28, 2024 12:13
@@ -42,6 +42,7 @@ def _(text):


logger = logging.getLogger(__name__)
os.path.altsep = '\\'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you define this as constant instead of overriding python library's internal property?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to OS_PATH_ALT_SEP.

@talhaaslam01 talhaaslam01 force-pushed the talha/upload-error branch 2 times, most recently from bcc6376 to 422227b Compare July 1, 2024 06:35
Copy link
Collaborator

@ziafazal ziafazal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@ziafazal ziafazal merged commit a5c8dce into overhangio:master Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants